package com.venta.autopartes.inicio;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.venta.autopartes.branch.Branch;
import com.venta.autopartes.car.Location;
import com.venta.autopartes.car.Vehicle;
import com.venta.autopartes.dao.BranchDAO;
import com.venta.autopartes.dao.LocationDAO;
import com.venta.autopartes.dao.UserDAO;
import com.venta.autopartes.dao.VehicleDAO;
import com.venta.autopartes.menu.LeftFloatingMenu;
import com.venta.autopartes.user.User;
import com.venta.autopartes.utils.Constants;
import com.venta.autopartes.utils.DataCatalog;
import com.venta.autopartes.utils.SessionFactory;
import com.venta.autopartes.utils.SessionValidator;

public class InicioAction extends ActionSupport  implements SessionAware {
	private List<Vehicle> vehicleList=null;
	private Map session;
	private int userId;
	private int carId;
	
	private DataCatalog dataCatalog;
	
	public String goHome() {
		//Map session=SessionFactory.getSession();
		session.clear();
		VehicleDAO vehicleDAO=new VehicleDAO();
		Branch branch=BranchDAO.getBranchByHostName("www.kukulkansystems.com");
		System.out.println("Let's go to Public Home");
		//String welcomeMessage=BranchDAO.getBranchMessage("www.kukulkansystems.com");
		session.put("branch", branch);
		
		vehicleList=vehicleDAO.getLastsVehiclesAdded(4);
		
		//session.put(Constants.VEHICLE_LIST, vehicleList);
		for(Vehicle vehicle:vehicleList){
			System.out.println(vehicle.getMake() +" "+ vehicle.getModel() + " ");
			for(String path:vehicle.getImagesList()){
				System.out.println("          " + path);
			}
		}
		
		return "success";

	}
	
	public String goPrivateHome() {
		
		if(session.get(Constants.DATA_CATALOG)==null){
			System.out.println("Getting data catalogs for vehicles, locations...");
			dataCatalog = new DataCatalog();
			session.put(Constants.DATA_CATALOG, dataCatalog);
		}
		
		if(!SessionValidator.isValidSession(session)){
			return SessionValidator.INVALID_SESSION;
		}
		
		List<Vehicle> vehiclesListSearch=null;
		List<User> usersListSearch=null;
		String result=SUCCESS;
		SessionFactory.removePreviosSessionData();
		
		User user=(User)session.get(Constants.USER_LOGGED);

		LeftFloatingMenu leftFloatingMenu= new LeftFloatingMenu();
		//System.out.println("Menu will be created!!");
		leftFloatingMenu.createLeftFloatingMenu();
		
        session.put("leftFloatingMenu",leftFloatingMenu);
		
		if(session.get("vehiclesListSearch")==null){
			System.out.println("Getting my available vehicles");
			VehicleDAO vehicleDAO= new VehicleDAO();
			vehiclesListSearch=vehicleDAO.getVehiclesByUser(user.getUserId());
			session.put("vehiclesListSearch", vehiclesListSearch);
			
			result=SUCCESS;
		}
		
		if(session.get("usersListSearch")==null){
			System.out.println("Getting my available subordinates");
			UserDAO userDAO = new UserDAO();
			usersListSearch=userDAO.getUsersByUser(user.getUserId());
			session.put("usersListSearch", usersListSearch);
			
			result=SUCCESS;
		}
		
		return result;

	}

	public List<Vehicle> getVehicleList() {
		return vehicleList;
	}

	public void setVehicleList(List<Vehicle> vehicleList) {
		this.vehicleList = vehicleList;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getCarId() {
		return carId;
	}

	public void setCarId(int carId) {
		this.carId = carId;
	}

	public DataCatalog getDataCatalog() {
		return dataCatalog;
	}

	public void setDataCatalog(DataCatalog dataCatalog) {
		this.dataCatalog = dataCatalog;
	}
	
	
	

}
